package com.example.soa_exp2.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.soa_exp2.Domain.Borrow;
import com.example.soa_exp2.Domain.Return;
import com.example.soa_exp2.Mapper.ReturnMapper;
import com.example.soa_exp2.Service.BorrowService;
import com.example.soa_exp2.Service.GoodsService;
import com.example.soa_exp2.Service.ReturnService;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ReturnServiceImpl implements ReturnService {
    @Resource
    private Return aReturn;

    @Resource
    private ReturnMapper returnMapper;

    @Resource
    private GoodsService goodsService;

    @Resource
    private BorrowService borrowService;

    @Override
    public List<Return> getReturnList(){
        return returnMapper.selectList(null);
    }

    @Override
    public Return getReturnById(int id) {
        QueryWrapper<Return> wrapper = new QueryWrapper<>();
        wrapper.eq("re_rec_id", id);
        return returnMapper.selectOne(wrapper);
    }

    @Override
    public List<Return> getReturnByGoodsId(String goods_id) {
        QueryWrapper<Return> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goods_id);
        return returnMapper.selectList(wrapper);
    }

    @Override
    public List<Return> getReturnByGoodsName(String name){
        QueryWrapper<Return> wrapper = new QueryWrapper<>();
        wrapper.like("goods_name", name);
        return returnMapper.selectList(wrapper);
    }

    @Override
    public List<Return> getReturnByStuId(String stu_id){
        QueryWrapper<Return> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stu_id);
        return returnMapper.selectList(wrapper);
    }

    @Override
    public List<Return> getReturnByStuName(String stu_name){
        QueryWrapper<Return> wrapper = new QueryWrapper<>();
        wrapper.like("stu_name", stu_name);
        return returnMapper.selectList(wrapper);
    }

    @Override
    public int addReturn(Return goods_return, int bor_rec_id) {
        if(goodsService.deleteGoodsStock(goods_return.getGoods_id(),goods_return.getRe_num()) != 0){
            borrowService.updateBorrowStatus(bor_rec_id);
            return returnMapper.insert(goods_return);
        }else {
            return 0;
        }
    }

    @Override
    public int deleteReturnById(int re_rec_id){
        return returnMapper.deleteById(re_rec_id);
    }

    @Override
    public int updateReturnById(Return re_rec){
        return returnMapper.updateById(re_rec);
    }

    @SneakyThrows
    @Override
    public List<Return> getReturnByTime(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date_start = date + " 00:00:00";
        String date_end = date + " 23:59:59";
        Date date_min = sdf.parse(date_start);
        Date date_max = sdf.parse(date_end);
        QueryWrapper<Return> wrapper = new QueryWrapper<>();
        wrapper.ge("re_time", date_min).le("re_time", date_max);
        return returnMapper.selectList(wrapper);
    }

    @SneakyThrows
    @Override
    public List<Return> getReturnByTime(String date_start, String date_end) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date_min = sdf.parse(date_start + " 00:00:00");
        Date date_max = sdf.parse(date_end + " 23:59:59");
        QueryWrapper<Return> wrapper = new QueryWrapper<>();
        wrapper.ge("re_time", date_min).le("re_time", date_max);
        return returnMapper.selectList(wrapper);
    }

}
